home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Add-Ons / MPW / MPW cawf 4.0.9 / bsfilt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-01  |  4.3 KB  |  228 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    bsfilt.c - a colcrt-like processor for cawf(1)
  3.  */
  4.  
  5. /*
  6.  *    Copyright (c) 1991 Purdue University Research Foundation,
  7.  *    West Lafayette, Indiana 47907.  All rights reserved.
  8.  *
  9.  *    Written by Victor A. Abell <abe@cc.purdue.edu>,  Purdue    University
  10.  *    Computing Center.  Not derived from licensed software; derived from
  11.  *    awf(1) by Henry Spencer of the University of Toronto.
  12.  *
  13.  *    Permission is granted to anyone to use this software for any
  14.  *    purpose on any computer system, and to alter it and redistribute
  15.  *    it freely, subject to the following restrictions:
  16.  *
  17.  *    1. The author is not responsible for any consequences of use of
  18.  *       this software, even if they arise from flaws in it.
  19.  *
  20.  *    2. The origin of this software must not be misrepresented, either
  21.  *       by explicit claim or by omission.  Credits must appear in the
  22.  *       documentation.
  23.  *
  24.  *    3. Altered versions must be plainly marked as such, and must not
  25.  *       be misrepresented as being the original software.  Credits must
  26.  *       appear in the documentation.
  27.  *
  28.  *    4. This notice may not be removed or altered.
  29.  */
  30.  
  31. #include <stdio.h>
  32.  
  33. #if    defined(STDLIB)
  34. #include <stdlib.h>
  35. #endif    /* defined(STDLIB) */
  36.  
  37. #if    defined(UNIX)
  38. # if    defined(USG)
  39. #include <string.h>
  40. # else    /* !defined(USG) */
  41. #include <strings.h>
  42. # endif    /* defined(USG) */
  43. #else    /* !defined(UNIX) */
  44. #include <string.h>
  45. #endif    /* defined(UNIX) */
  46.  
  47. #include <sys/types.h>
  48.  
  49. #include "ansi.h"
  50.  
  51. #define MAXLL    2048            /* ridiculous maximum line length */
  52.  
  53. int Dash = 1;                /* underline with dashes */
  54. int Dp = 0;                /* dash pending */
  55. int Lc = 0;                /* line count */
  56. char *Pname;                /* program name */
  57. unsigned char Ulb[MAXLL];        /* underline buffer */
  58. int Ulx = 0;                /* underline buffer index */
  59.  
  60. _PROTOTYPE(void Putchar,(int ch));
  61.  
  62. main(argc, argv)
  63.     int argc;
  64.     char *argv[];
  65. {
  66.     int ax = 1;            /* argument index */
  67.     unsigned char c;        /* character buffer */
  68.     FILE *fs;            /* file stream */
  69.     int nf = 0;            /* number of files processed */
  70.     unsigned char pc;        /* previous character */
  71.     int under = 0;                  /* underline */
  72.  
  73. #if    defined(macintosh)
  74. /*
  75.  * Perform Macintosh intialization.
  76.  */
  77.     extern void InitToolBox();
  78.  
  79.     InitToolbox();
  80. #endif    /* defined(macintosh) */
  81.  
  82. /*
  83.  * Save program name.
  84.  */
  85.     if ((Pname = strrchr(argv[0], '/')) != NULL)
  86.         Pname++;
  87.     else if ((Pname = strrchr(argv[0], '\\')) != NULL)
  88.         Pname++;
  89.     else
  90.         Pname = argv[0];
  91. /*
  92.  * Process options.
  93.  */
  94.     if (argc > 1 && argv[1][0] == '-') {
  95.         switch (argv[1][1]) {
  96.     /*
  97.      * "-U" - underline with dashes.
  98.      */
  99.         case 'U':
  100.             Dash = 0;
  101.             under = 1;
  102.             break;
  103.     /*
  104.      * "-" - do no  underlining at all.
  105.      */
  106.         case '\0':
  107.             Dash = under = 0;
  108.             break;
  109.         default:
  110.             (void) fprintf(stderr,
  111.                 "%s usage: [-] [-U] [file]\n", Pname);
  112.             exit(1);
  113.         }
  114.         ax++;
  115.     }
  116. /*
  117.  * Process files.  Read standard input if no files names.
  118.  */
  119.  
  120.     while (ax < argc || nf == 0) {
  121.         if (ax >= argc)
  122.             fs = stdin;
  123.         else {
  124.  
  125. #if    defined(UNIX)
  126.             if ((fs = fopen(argv[ax], "r")) == NULL)
  127. #else    /* !defined(UNIX) */
  128.             if ((fs = fopen(argv[ax], "rt")) == NULL)
  129. #endif    /* defined(UNIX) */
  130.  
  131.             {
  132.                 (void) fprintf(stderr, "%s: can't open %s\n",
  133.                     Pname, argv[ax]);
  134.                 exit(1);
  135.             }
  136.             ax++;
  137.         }
  138.         nf++;
  139.     /*
  140.      * Read input a character at a time.
  141.      */
  142.         for (pc = '\0';;) {
  143.             c = (unsigned char)fgetc(fs);
  144.             if (feof(fs))
  145.                 break;
  146.             switch(c) {
  147.  
  148.             case '\n':
  149.                 if (pc)
  150.                     Putchar((int)pc);
  151.                 Putchar('\n');
  152.                 pc = '\0';
  153.                 break;
  154.  
  155.             case '\b':
  156.                 if (pc == '_') {
  157.                     if (under) {
  158.                         putchar(pc);
  159.                         putchar('\b');
  160.                     } else if (Dash)
  161.                         Dp = 1;
  162.                 }
  163.                 pc = '\0';
  164.                 break;
  165.  
  166.             default:
  167.                 if (pc)
  168.                     Putchar((int)pc);
  169.                 pc = c;
  170.             }
  171.         }
  172.         if (pc) {
  173.             Putchar((int)pc);
  174.             Putchar((int)'\n');
  175.         }
  176.     }
  177.     return(0);
  178. }
  179.  
  180.  
  181. /*
  182.  * Putchar(ch) - put a character with possible underlining
  183.  */
  184.  
  185. void
  186. Putchar(ch)
  187.     int ch;
  188. {
  189.     int i;                    /* temporary index */
  190.  
  191.     if ((unsigned char)ch == '\n') {
  192. /*
  193.  * Handle end of line.
  194.  */
  195.         putchar('\n');
  196.         if (Ulx) {
  197.             while (Ulx && Ulb[Ulx-1] == ' ')
  198.                 Ulx--;
  199.             if (Ulx) {
  200.                 for (i = 0; i < Ulx; i++)
  201.                     putchar(Ulb[i]);
  202.                 putchar('\n');
  203.             }
  204.         }
  205.         Dp = Ulx = 0;
  206.         Lc++;
  207.         return;
  208.     }
  209. /*
  210.  * Put "normal" character.
  211.  */
  212.     putchar((unsigned char)ch);
  213.     if (Dash) {
  214.  
  215.     /*
  216.      * Handle dash-type underlining.
  217.      */
  218.         if (Ulx >= MAXLL) {
  219.             (void) fprintf(stderr,
  220.                 "%s: underline for line %d > %d characters\n",
  221.                 Pname, Lc, MAXLL);
  222.             exit(1);
  223.         }
  224.         Ulb[Ulx++] = Dp ? '-' : ' ';
  225.         Dp = 0;
  226.     }
  227. }
  228.